package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdAtta;
import com.bwie.service.TbProdAttaService;
import com.bwie.mapper.TbProdAttaMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 31439
* @description 针对表【tb_prod_atta】的数据库操作Service实现
* @createDate 2025-07-22 18:55:14
*/
@Service
public class TbProdAttaServiceImpl extends ServiceImpl<TbProdAttaMapper, TbProdAtta>
    implements TbProdAttaService{

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    TbProdAttaMapper tbProdAttaMapper;

    @Override
    public R addProdAtta(TbProdAtta tbProdAtta) {

        //先判断附件是否存在
        TbProd tbProd = tbProdMapper.selectById(tbProdAtta.getProdId());
        if(tbProd == null){
            return R.ERROR(500,"商品不存在");
        }
        //删除原有附件，根据商品ID删除
        tbProdAttaMapper.delete(new QueryWrapper<TbProdAtta>().lambda()
                .eq(TbProdAtta::getProdId,tbProdAtta.getProdId()));
        //添加附件
        tbProdAttaMapper.insert(tbProdAtta);
        return R.OK();
    }
}




